<?php

namespace App\Http\Controllers\Home\Cart;

use Illuminate\Http\Request;


use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;


class CartController extends Controller
{
    
	//购物车列表页面
	public function getIndex()
	{
		if(!empty(session('cart'))){
			$carts = session('cart');
			$data = [];
			
			foreach ($carts as $k => $v) {
				//查询商品数据
				$tmp = DB::table('store_goods_info')
					->select('goods_info_name','id','goods_info_price','goods_info_img','goods_info_type')
					->where('id',$v['goods_id'])
					->first();	  
				$tmp->cart_num = $v['cart_num'];
				$data[] = $tmp;
				//dd($data);
			}
			//解析模板
			return view('home.cart.index',['carts'=>$data]);
		}else{
			return view('home.cart.index');
		}	
	}


    //购物车添加操作
    public function postInsert(Request $request)
    {
    	//提取商品id和数量
    	$data = $request->only('goods_id','cart_num');

    	//检测商品是否存在于购物车中
    	$res = $this->check($data['goods_id'],$data['cart_num']);
    	
    	if($res){
    		//将修改后的购物车重写入session
    		session(['cart'=>$res]);
    	}else{
	    	//将数据压入到session的cart的数组中
	    	$request->session()->push('cart',$data);
    	}



    	//跳转到列表页
    	return redirect('/cart/index');
	}


	//定义方法来检测商品是否存在购物车中
	public function check($goods_id,$cart_num)
	{
		//提取购物车信息
		$goods = session('cart');
		if(empty($goods)){
			return false;
		}

		//遍历购物车
		foreach($goods as $k=>$v){
			//如果商品存在当前购物车中,修改购物车数据,并返回购物车
			if($v['goods_id'] == $goods_id){
				$goods[$k]['cart_num'] += $cart_num;
				return $goods;
			}
		}

		//如果session都不等于$goods_id
		return false;
		
	}
	

	//将购物车传过来的商品数量存入session
	public function postNum(Request $request)
	{
		$data = $request->only('i'); 
		$res = $request->only(['goods_id','cart_num']);
		$tmp = $request->session()->get('cart');
		$tmp[$data['i']] = $res;
		
		session(['cart'=>$tmp]);
		
		
	}

	//删除购物车商品
	public function postClear(Request $request)
	{
		//获取要删除的下标
		$data = $request->only('i');
		//获取session 
		$tmp = $request->session()->get('cart');
		//移除指定商品
		array_pull($tmp,$data['i']);

		$array = [];
		//遍历移除商品后的session
		foreach($tmp as $k=>$v){
			$arr['goods_id'] = $v['goods_id'];
			$arr['cart_num'] = $v['cart_num'];
			$array[] = $arr;
		}
		session(['cart'=>$array]);
		if(empty($array)){
			return 0;
		}
		return;


		// $id = [];
		// //遍历删除商品后的session
		// foreach($tmp as $k=>$v){
		// 	//插入数据库 并查询id
		// 	$id[] = DB::table('store_cart')->insertGetId($v);
		// }
		// //获取数组中元素个数
		// $num = count($id);
		// if($num >= 2){
			
		// 	//有两个以上商品是执行
		// 	$data = DB::table('store_cart')->select('goods_id','cart_num')->whereBetween('id',$id)->get();
		// 	//对象转数组
		// 	foreach($data as $object)
		// 	{
		// 	    $arrays[] =  (array) $object;
		// 	}
		// 	session(['cart'=>$arrays]);
		// 	return;
		// }elseif($num == 1){
			
		// 	//如果有一个
		// 	$data = DB::table('store_cart')->select('goods_id','cart_num')->where('id',$id)->get();
		// 	foreach($data as $object)
		// 	{
		// 	    $arrays[] =  (array) $object;
		// 	}
		// 	session(['cart'=>$arrays]);
		// 	return;
		// }elseif($num == 0){

			
		// 	$request->session()->forget('cart');

		// 	return;
		// }
	}

	

}
